package com.bobo.controller.admin;

import com.bobo.dto.VegetableDTO;
import com.bobo.dto.VegetablePageQueryDTO;
import com.bobo.entity.Vegetable;
import com.bobo.result.PageResult;
import com.bobo.result.Result;
import com.bobo.service.VegetableService;
import com.bobo.vo.VegetableVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags="菜品管理接口")
@Slf4j
public class VegetableController {
    @Autowired
    private VegetableService vegetableService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 新增菜品
     * @param vegetableDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody VegetableDTO vegetableDTO){
        log.info("新增菜品", vegetableDTO);
        vegetableService.savewithFlavor(vegetableDTO);
        //清理缓存数据
        String key = "dish_"+ vegetableDTO.getId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param vegetablePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(VegetablePageQueryDTO vegetablePageQueryDTO){
        log.info("菜品分特查询{}", vegetablePageQueryDTO);
        PageResult pageResult = vegetableService.pageQuery(vegetablePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 菜品的批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result delete(@RequestParam List<Long> ids){
        log.info("菜品批量删除{}",ids);
        vegetableService.deleteBatch(ids);
        //将所有菜品缓存清理dish_开头的数据
        cleanCache("dish_*");
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<VegetableVO> getByid(@PathVariable Long id){
        log.info("根据id查询菜品：{}",id);
        VegetableVO vegetableVO = vegetableService.getByIdWithFlavor(id);
        return Result.success(vegetableVO);
    }
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody VegetableDTO vegetableDTO){
        log.info("修改菜品：{}", vegetableDTO);
        vegetableService.updateWithFlavor(vegetableDTO);
        //全杀了
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Vegetable>> list(Long categoryId){
        log.info("根据分类id查询菜品{}",categoryId);
        List<Vegetable> list = vegetableService.list(categoryId);
        return Result.success(list);
    }

    /**
     * 菜品起售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售停售")
    public Result<String> startOrStop(@PathVariable Integer status,Long id){
        log.info("菜品起售停售{}",id);
        vegetableService.startOrStop(status,id);
        //杀杀杀
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
